package com.woniuxy.boot.nb_auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.boot.common.dto.IdCardDTO;
import com.woniuxy.boot.nb_auth.bean.IdCard;
import com.woniuxy.boot.nb_auth.bean.User;
import com.woniuxy.boot.nb_auth.exception.*;
import com.woniuxy.boot.nb_auth.feign.DistributedIdFeign;
import com.woniuxy.boot.nb_auth.mapper.IdCardMapper;
import com.woniuxy.boot.nb_auth.mapper.UserMapper;
import com.woniuxy.boot.nb_auth.pojo.RealNamePojo;
import com.woniuxy.boot.nb_auth.service.IdCardService;
import com.woniuxy.boot.nb_auth.util.RealName;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

@Service
public class IdCardServiceImpl extends ServiceImpl<IdCardMapper, IdCard> implements IdCardService {
    @Autowired
    private IdCardMapper idCardMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DistributedIdFeign distributedIdFeign;

    @Autowired
    private RealName realName;

/*    @Transactional
    @Override
    public void bindIdCard(IdCard idCard)throws IOException{
        try {
            RealNamePojo realNamePojo = realName.postData(idCard.getName(), idCard.getIdCard());
            switch (realNamePojo.getRespCode()) {
                case "0000":
                    idCard.setId(distributedIdFeign.get());
                    idCard.setStatus("a");
                    updateUserStatus(idCard.getUserId());
                    idCardMapper.insert(idCard);
                    break;
                case "0001":
                    throw new RealNameNoMatchingException("开户名不能为空");
                case "0002":
                    throw new RealNameNoMatchingException("开户名不能包含特殊字符");
                case "0003":
                    throw new RealNameNoMatchingException("身份证号码不能为空");
                case "0004":
                    throw new RealNameNoMatchingException("身份证号码格式错误");
                case "0007":
                    throw new RealNameNoMatchingException("无此身份证号码");
                case "0008":
                    throw new RealNameNoMatchingException("身份证信息不匹配");
                default:
                    throw new RuntimeException();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }*/

    @Override
    public IdCardDTO getUserIdCard(Long userId) {
        LambdaQueryWrapper<IdCard> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(IdCard::getUserId, userId);
        IdCard idCard = idCardMapper.selectOne(lambdaQueryWrapper);
        IdCardDTO idCardDTO = new IdCardDTO();
        BeanUtils.copyProperties(idCard, idCardDTO);
        return idCardDTO;
    }


    @Override
    @Transactional
    public void bindIdCard(IdCard idCard) throws IOException {
        //先根据用户的id查询有无已经绑定的身份证
        if (IsBindIdCard(idCard.getUserId())) {
            throw new IdCardIsBindException("用户已绑定身份证");
        }
            //根据realNamePojo的不同状态码进行不同的处理
            RealNamePojo realNamePojo = realName.postData(idCard.getName(), idCard.getIdCard());
            handleResponseCode(realNamePojo.getRespCode());

            idCard.setId(distributedIdFeign.get());
            idCard.setStatus("a");
            //修改用户的实名状态
            updateUserStatus(idCard.getUserId());
            idCardMapper.insert(idCard);

    }

    /**
     * 根据realNamePojo的不同状态码进行不同的处理
     *
     * @param respCode
     */
    private void handleResponseCode(String respCode) {
        switch (respCode) {
            case "0000":
                return; // Success, do nothing.
            case "0001":
                throw new NameIsNullException("开户名不能为空");
            case "0002":
                throw new NameHaveSpecialCharException("开户名不能包含特殊字符");
            case "0003":
                throw new IdCardIsNullException("身份证号码不能为空");
            case "0004":
                throw new IdCardWrongsFormatException("身份证号码格式错误");
            case "0007":
                throw new IdCardIsVoidException("无此身份证号码");
            case "0008":
                throw new RealNameNoMatchingException("身份证信息不匹配");
            case "0010":
                throw new SystemUpholdException("系统维护中,请稍后再试");
            default:
                throw new RuntimeException("未知的实名验证响应码：" + respCode);
        }
    }

    private boolean IsBindIdCard(Long userId) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getId, userId);
        User user = userMapper.selectOne(lambdaQueryWrapper);
        return "b".equals(user.getStatus());
    }


    /**
     * 修改用户的实名状态
     *
     * @param userId
     */
    private void updateUserStatus(Long userId) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getId, userId);
        User user = userMapper.selectOne(lambdaQueryWrapper);
        user.setStatus("b");
        userMapper.insertOrUpdate(user);
    }
}
